package com.li.drug.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.li.drug.entity.Problem;
import com.li.drug.service.ProblemService;
import com.li.drug.utils.ResultMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**p
 * 问题药品相关controller
 */
@Controller
@RequestMapping("/problem")
public class ProblemController {

    @Autowired
    ProblemService problemService;

    /**
     * 转向问题药品页面
     * @return
     */
    @RequestMapping
    public String problem(){
        return "problem/problem";
    }

    //分页查询问题药品列表
    @RequestMapping("/problemQueryPage")
    @ResponseBody
    public Object problemQueryPage(String param,
                                    @RequestParam(name = "page") int pageNum,
                                    @RequestParam(name = "limit") int pageSize){
        try {
            Page<Problem> page = problemService.selectProblemPage(pageNum,pageSize,param);
            return ResultMapUtil.getHashMapMysqlPage(page);

        }catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向问题药品新增页面
     * @return
     */
    @RequestMapping("/problemPage")
    public String problemPage(){
        return "problem/problemPage";
    }

    /**
     * 添加一个问题药品
     */
    @RequestMapping("/problemAdd")
    @ResponseBody
    public Object problem(Problem problem){
        problem.setCreatetime(new Date());
        int i = problemService.addProblem(problem);
        return ResultMapUtil.getHashMapSave(i);
    }

    /**
     * 转向问题药品编辑页面
     * @return
     */
    @RequestMapping("/problemQueryById")
    public String problemEditById(@RequestParam(name = "id") Integer id,
                                   Model model){
        Problem problem = problemService.getProblemById(id);
        model.addAttribute("problem",problem);
        return "problem/problemPage";
    }

    /**
     * 修改一个问题药品信息
     */
    @RequestMapping("/problemEdit")
    @ResponseBody
    public Object problemEdit(Problem problem){
        try {
            problem.setCreatetime(new Date());
            int i = problemService.editProblem(problem);
            return ResultMapUtil.getHashMapUpdate(i);
        }catch (Exception e){
            return  ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个问题药品信息
     */
    @RequestMapping("/problemDelById")
    @ResponseBody
    public Object problemDelById(@RequestParam(name = "id") Integer id){
        try {
            int i = problemService.delProblemById(id);
            return ResultMapUtil.getHashMapDel(i);
        }catch (Exception e){
            return  ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 获取所有问题药品列表
     */
    @RequestMapping("/problemList")
    @ResponseBody
    public Object problemList(){
        List<Problem> list = problemService.list();
        return list;
    }
}
